package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.csun.gateway.entity.CsunGatewayData;
import com.csun.cmny.provider.model.domain.DmcCsunGatewayConfigHumidistat;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcDeviceOnlineRecord;
import com.csun.cmny.provider.model.dto.bodysensor.BindCsunBodySensorLocationDto;
import com.csun.cmny.provider.model.dto.bodysensor.CsunBodySensorPageQuery;
import com.csun.cmny.provider.model.dto.device.DeviceBindDto;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.model.vo.csunbody.DmcCsunBodySensorVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunBodySensorLocationService;
import com.csun.cmny.provider.service.DmcDeviceOnlineRecordService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.utils.DeviceUtils;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 13:22
 * @Description: the class dmc device body sensor controller.
 */
@RestController
@RequestMapping(value = "/bodySensor", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcBodySensorController extends BaseController {

    @Resource
    private DeviceUtils deviceUtils;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcCsunBodySensorLocationService dmcCsunBodySensorLocationService;

    @Resource
    private DmcDeviceOnlineRecordService dmcDeviceOnlineRecordService;



    /**
     * 查询组织人体感应设备列表
     *
     * @param csunBodySensorPageQuery   the csun body sensor page query
     *
     * @return  the wrapper
     */
    //@LogAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询组织人体感应设备列表")
    @RequestMapping(value = "/queryGroupBodySensorListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupBodySensorListWithPage(@RequestBody CsunBodySensorPageQuery csunBodySensorPageQuery) {

        csunBodySensorPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        csunBodySensorPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex());

        PageInfo pageInfo = dmcCsunBodySensorLocationService
                .queryGroupBodySensorListWithPage(csunBodySensorPageQuery);
        for (Object o : pageInfo.getList()) {
            DmcCsunBodySensorVo dmcCsunBodySensorVo = (DmcCsunBodySensorVo) o;
            if (!PubUtils.isNull(dmcCsunBodySensorVo.getBindType())) {
                dmcCsunBodySensorVo.setBind(true);
                DeviceBindDto deviceBindDto = new DeviceBindDto();
                BeanUtils.copyProperties(dmcCsunBodySensorVo, deviceBindDto);
                RoomBuildingVo rbv = deviceUtils.getRoomBuildingVo(deviceBindDto);
                if (PublicUtil.isNotEmpty(rbv) && PublicUtil.isNotEmpty(rbv.getLocation())) {
                    dmcCsunBodySensorVo.setLocation(rbv.getLocation());
                    dmcCsunBodySensorVo.setBuildingName(rbv.getBuildingName());
                    dmcCsunBodySensorVo.setFloorNo(rbv.getFloorNo());
                    dmcCsunBodySensorVo.setRoomNo(rbv.getRoomNo());
                    dmcCsunBodySensorVo.setOldmanId(rbv.getOldmanId());
                    dmcCsunBodySensorVo.setOldmanName(rbv.getOldmanName());
                }
            }
        }

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加人体感应设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加人体感应设备")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addBodySensor(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改人体感应设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改人体感应设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editBodySensor(@Validated(value = {DmcDeviceEditValidator.class})
                                  @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除人体感应
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除人体感应")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcBodySensorById(@PathVariable Long id) {

        Integer result = dmcCsunBodySensorLocationService.deleteBodySensorById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        DmcDeviceOnlineRecord dmcDeviceOnlineRecord = new DmcDeviceOnlineRecord();
        dmcDeviceOnlineRecord.setDeviceId(id);
        int delete = dmcDeviceOnlineRecordService.delete(dmcDeviceOnlineRecord);
       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 绑定人体感应
     *
     * @param bindCsunBodySensorLocationDto bind csun body sensor location dto
     *
     * @return the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/bindCsunBodySensorLocation", method = RequestMethod.POST)
    public Wrapper bindCsunBodySensorLocation(@Validated @RequestBody BindCsunBodySensorLocationDto
                                                          bindCsunBodySensorLocationDto, BindingResult br) {
        Integer result = dmcCsunBodySensorLocationService
                .bindCsunBodySensorLocation(bindCsunBodySensorLocationDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 解绑人体感应
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/breakCsunBodySensorLocation/{deviceId}", method = RequestMethod.POST)
    public Wrapper breakCsunBodySensorLocation(@PathVariable Long deviceId) {

        Integer result = dmcCsunBodySensorLocationService.breakCsunBodySensorLocation(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        DmcDeviceOnlineRecord dmcDeviceOnlineRecord = new DmcDeviceOnlineRecord();
        dmcDeviceOnlineRecord.setDeviceId(deviceId);
        int delete = dmcDeviceOnlineRecordService.delete(dmcDeviceOnlineRecord);

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

}
